package UTS.model;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Vincentius HMK
 */
public class Surat {
    private String nomor, perihal, asal, penerima, letak;
    private Date tanggalDiterima;
    private File fileScan;
    private String pilihanSuratDokumen;
    private String statusSurat;

    public Surat() {
    }

    /**
     * @return the nomor
     */
    public String getNomor() {
        return nomor;
    }

    /**
     * @param nomor the nomor to set
     */
    public void setNomor(String nmr)  {
        if (nmr.length() == 3) {
            nomor = nmr;
        } else {
            JOptionPane.showMessageDialog(null, "Nomor harus 3 karakter");
        }
    }

    /**
     * @return the perihal
     */
    public String getPerihal() {
        return perihal;
    }

    /**
     * @param perihal the perihal to set
     */
    public void setPerihal(String perihal) {
        this.perihal = perihal;
    }

    /**
     * @return the asal
     */
    public String getAsal() {
        return asal;
    }

    /**
     * @param asal the asal to set
     */
    public void setAsal(String asal) {
        this.asal = asal;
    }

    /**
     * @return the penerima
     */
    public String getPenerima() {
        return penerima;
    }

    /**
     * @param penerima the penerima to set
     */
    public void setPenerima(String penerima) {
        this.penerima = penerima;
    }

    /**
     * @return the letak
     */
    public String getLetak() {
        return letak;
    }

    /**
     * @param letak the letak to set
     */
    public void setLetak(String letak) {
        this.letak = letak;
    }

    /**
     * @return the tanggalDiterima
     */
    public Date getTanggalDiterima() {
        return tanggalDiterima;
    }

    /**
     * @param tanggalDiterima the tanggalDiterima to set
     */
    public void setTanggalDiterima(Date tanggalDiterima) {
        this.tanggalDiterima = tanggalDiterima;
    }

    /**
     * @return the fileScan
     */
    public File getFileScan() {
        return fileScan;
    }

    /**
     * @param fileScan the fileScan to set
     */
    public void setFileScan(File fileScan) {
        this.fileScan = fileScan;
    }

    /**
     * @return the pilihanSuratDokumen
     */
    public String getPilihanSuratDokumen() {
        return pilihanSuratDokumen;
    }

    /**
     * @param pilihanSuratDokumen the pilihanSuratDokumen to set
     */
    public void setPilihanSuratDokumen(String pilihanSuratDokumen) {
        this.pilihanSuratDokumen = pilihanSuratDokumen;
    }

    public static ResultSet getDaftarSurat(java.sql.Date mulai, java.sql.Date akhir) throws SQLException {
        String mul = mulai.toString();
        String akh = akhir.toString();
        String sql = "select * from surat where tanggalDiterima between ?  and  ? ";
//         Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS085314016", "MHS085314016");
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "SYSTEM", "MHS085314016");
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setDate(1, mulai);
        ps.setDate(2, akhir);
        ResultSet results = ps.executeQuery();
        return results;
    }

    /**
     * @return the statusSurat
     */
    public String getStatusSurat() {
        return statusSurat;
    }

    /**
     * @param statusSurat the statusSurat to set
     */
    public void setStatusSurat(String statusSurat) {
        this.statusSurat = statusSurat;
    }

    public void updateStatusSurat(){
        try {
            String sql = "UPDATE SURAT set status = ? where nomor = ?";
//             Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS085314016", "MHS085314016");
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "SYSTEM", "MHS085314016");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, this.statusSurat);
            ps.setString(2, this.nomor);
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Surat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}
